package com.hyt.it.ogt.pj.data.service.kwgov.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.pj.data.common.Constants;
import com.hyt.it.ogt.pj.data.config.ConfigManager;
import com.hyt.it.ogt.pj.data.mapper.kwgov.KwGovTaskMapper;
import com.hyt.it.ogt.pj.data.model.entity.kwgov.Task;
import com.hyt.it.ogt.pj.data.model.vo.kwgov.NowTestNowDataVO;
import com.hyt.it.ogt.pj.data.model.vo.kwgov.TestClassifyVO;
import com.hyt.it.ogt.pj.data.model.vo.pt.ExaminationCategoryDTO;
import com.hyt.it.ogt.pj.data.model.vo.pt.MultiTree;
import com.hyt.it.ogt.pj.data.service.kwgov.IKwGovTaskService;
import com.hyt.it.ogt.pj.data.service.pt.IExaminationCategoryService;
import com.hyt.it.ogt.pj.data.utils.DateTimeUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 考试任务表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@Slf4j
@Service
public class KwGovTaskServiceImpl extends BaseServiceImpl<KwGovTaskMapper, Task> implements IKwGovTaskService {
	
	@Resource
	private IExaminationCategoryService iExaminationCategoryService;
	
	@Resource
	RedisTemplate<String, String> redisTemplate;
	
	@Resource
	private ConfigManager configManager;

	@DS("kw_gov_read")
	@Override
	public Task getTaskById(String id) {
		QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();
		taskQueryWrapper.lambda().eq(Task::getId, id).eq(Task::getDelFlag, false);
		return getOne(taskQueryWrapper);
	}

	@DS("kw_gov_read")
	@Override
	public NowTestNowDataVO getTaskExamType(String taskId) {
		// 获取任务数据
		QueryWrapper<Task> taskQueryWrapper = new QueryWrapper<>();
		taskQueryWrapper.lambda().eq(Task::getId, taskId).eq(Task::getDelFlag, false);
		Task task = getOne(taskQueryWrapper);
		NowTestNowDataVO nowTestNowDataVO = new NowTestNowDataVO();
		nowTestNowDataVO.setBeginDate(DateTimeUtil.getDateTimeString(task.getBeginDate()));
		nowTestNowDataVO.setEndDate(DateTimeUtil.getDateTimeString(task.getEndDate()));
		nowTestNowDataVO.setExamName(task.getName());
		nowTestNowDataVO.setId(taskId);

		nowTestNowDataVO.setTestClassify(task.getTestClassify());
		List<TestClassifyVO> testClassifyVOList = getTestClassify();
		if (null != testClassifyVOList) {
			setTestClassify(testClassifyVOList, task.getTestClassify(), nowTestNowDataVO);
		}
		return nowTestNowDataVO;
	}

	private void setTestClassify(List<TestClassifyVO> testClassifyVOList, String taskTestClassify, NowTestNowDataVO nowTestNowDataVO) {
		testClassifyVOList.stream().forEach(testClassifyVO -> {
			if (taskTestClassify.contains(testClassifyVO.getId())) {
				if (StringUtils.isBlank(nowTestNowDataVO.getTestClassifyName())) {
					nowTestNowDataVO.setTestClassifyName(testClassifyVO.getName());
				} else {
					nowTestNowDataVO.setTestClassifyName(StringUtils.join(nowTestNowDataVO.getTestClassifyName(), ",", testClassifyVO.getName()));
				}
				if (testClassifyVO.getChildren() != null && !testClassifyVO.getChildren().isEmpty()) {
					setTestClassify(testClassifyVO.getChildren(), taskTestClassify, nowTestNowDataVO);
				}
			}
		});
	}

	@Override
	public List<TestClassifyVO> getTestClassify() {
		String ptClassifyTreeValue = redisTemplate.opsForValue().get(Constants.PT_CLASSIFY_TREE_KEY);
		if(StringUtils.isNoneBlank(ptClassifyTreeValue)) {
			return JSON.parseArray(ptClassifyTreeValue, TestClassifyVO.class);
		}
		MultiTree<ExaminationCategoryDTO> res = iExaminationCategoryService.getTreelist();
		if (res == null) {
			log.info("获取平台端考试分类出现空异常，getTestClassify；");
		}
		TestClassifyVO testClassifyVO = null;
		try {
			String treeJson = JSON.toJSONString(res);
			testClassifyVO = JSON.parseObject(treeJson, TestClassifyVO.class);
		} catch (Exception e) {
			log.error("解析异常", e);
		}
		if (null != testClassifyVO) {
			// 设置children未null，前端需要
			List<TestClassifyVO> testClassifyVOList = testClassifyVO.getChildren();
			setTreeChildrenNull(testClassifyVOList);
			String treeJson = JSON.toJSONString(testClassifyVOList);
			// 添加数据缓存
			redisTemplate.opsForValue().set(Constants.PT_CLASSIFY_TREE_KEY, treeJson, configManager.getRedisDataInvalidTime(), TimeUnit.SECONDS);
			return testClassifyVOList;
		}
		return null;
	}

	private void setTreeChildrenNull(List<TestClassifyVO> testClassifyVOList) {
		testClassifyVOList.stream().forEach(testClassifyVO -> {
			if (testClassifyVO.getChildren() == null) {
				return;
			}
			if (testClassifyVO.getChildren().isEmpty()) {
				testClassifyVO.setChildren(null);
			} else {
				setTreeChildrenNull(testClassifyVO.getChildren());
			}
		});
	}

}
